using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GameAOT;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 禁止UIMapManager以外的地方创建UIN_BaseMapSymbol实例 全部由UIMapManager内部池子管理
    /// </summary>
    public partial class UIMapManager
    {
        private class UINormalMapSymbol : UIBaseMapSymbol
        {
            #region Template Generate,don't modify

            protected class UIB_UIN_NormalMapSymbol
            {
                #region ObjectBinding Generate

                public ExButton btn { protected set; get; }
                public ExImage icon { protected set; get; }

                public virtual void InitBinding(ObjectBinding __binding)
                {
                    __binding.TryGetVariableValue<ExButton>("btn", out var __tbv0);
                    btn = __tbv0;
                    __binding.TryGetVariableValue<ExImage>("icon", out var __tbv1);
                    icon = __tbv1;
                }

                #endregion ObjectBinding Generate
            }

            #endregion Template Generate,don't modify


            #region properties

            protected UIB_UIN_NormalMapSymbol ui { get; set; }

            #endregion properties

            #region fields

            private ExButton _btn;
            private ExImage _icon;

            #endregion fields

            protected override void BeforeInit()
            {
                ui = new UIB_UIN_NormalMapSymbol();
                ui.InitBinding(csObjBind);
            }

            protected override void OnInit()
            {

            }
            
            protected override void AddUGUIListener()
            {
                ui.btn.AddClickEvent(OnInteracted);
            }

            protected override void Draw()
            {
                var curArea = GameplayAreaManager.Instance.curArea as IGameplaySceneArea;
                var entity = curArea?.GetTrackingEntity();
                var trackingEntityId = entity == null ? 0 : entity.id;
                ui.icon.maskable = owner.curContainer is not UIN_MiniMap || trackingEntityId != data.entityId;
                ui.icon.raycastTarget = owner.curContainer is not UIN_MiniMap;
                UpdateTransform();
                SetSymbolIcon().Forget();
            }

            private async UniTaskVoid SetSymbolIcon()
            {
                //避免icon大小变化导致的闪烁
                ui.icon.SetActive(false);
                var assetPath = data.iconPath;
                if (string.IsNullOrEmpty(data.config.Icon))
                    return;
                var sprite = await this.resLoader.LoadABAssetAwait<Sprite>(assetPath);
                //仅接受和当前数据iconPath匹配的结果
                if (data == null || data.iconPath != assetPath)
                {
                    return;
                }

                ui.icon.sprite = sprite;
                SetSymbolIconSize();
                ui.icon.SetActive(true);
            }

            private void SetSymbolIconSize()
            {
                if (size == Vector2.zero)
                {
                    var w = ui.icon.overrideSprite.rect.width / ui.icon.pixelsPerUnit;
                    var h = ui.icon.overrideSprite.rect.height / ui.icon.pixelsPerUnit;
                    size = new Vector2(w, h);
                }

                ui.icon.rectTransform.anchorMax = transform.anchorMin;
                ui.icon.rectTransform.sizeDelta = size;
                ui.icon.SetAllDirty();
            }
        }
    }
}